package com.xszx.dao.impl;

import com.xszx.bean.User;
import com.xszx.dao.UserDao;
import com.xszx.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.sql.Date;
import java.util.List;

public class UserDaoImpl implements UserDao {

    @Override
    public User login(String username, String password) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_user where username = ? and password = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String realName = resultSet.getString("realname");
                String cardId = resultSet.getString("cardid");
                Date birthday = resultSet.getDate("birthday");
                String phone = resultSet.getString("phone");
                String userInfo = resultSet.getString("userInfo");

                User user = new User(id, username, realName, cardId, birthday, phone, userInfo);
                return user;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<User> getAllUser() {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_user";
            PreparedStatement ps = connection.prepareStatement(sql);
            ResultSet resultSet = ps.executeQuery();

            List<User> list = new ArrayList<>();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String username = resultSet.getString("username");
                String realName = resultSet.getString("realname");
                String cardId = resultSet.getString("cardid");
                Date birthday = resultSet.getDate("birthday");
                String phone = resultSet.getString("phone");
                String userInfo = resultSet.getString("userInfo");

                User user = new User(id, username, realName, cardId, birthday, phone, userInfo);
                list.add(user);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean registerUser(String username, String phone, String password) {
        try {
            if (existManagerName(username)) {
                return false;
            }
            Connection connection = JDBCUtil.getConnection();
            String sql = "insert into t_user(id, username, password, phone) values(null, ?, ?, ?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ps.setString(3, phone);
            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    //判断用户名是否已经存在
    private boolean existManagerName(String username) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_user where username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);

            ResultSet resultSet = ps.executeQuery();
            if (resultSet.next()) {
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    @Override
    public void updateUser(User user) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update t_user set username = ?, realname = ?, cardid = ?, phone = ?, userInfo = ? where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getRealName());
            ps.setString(3, user.getCardId());
            ps.setString(4, user.getPhone());
            ps.setString(5, user.getUserInfo());
            ps.setInt(6, user.getId());

            ps.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void updatePassword(Integer id, String oldPassword, String newPassword) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "update t_user set password = ? where id = ? and password = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, newPassword);
            ps.setInt(2, id);
            ps.setString(3, oldPassword);
            ps.execute();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Integer getUserIdByUsername(String username) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select id from t_user where username = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()) {
                int userId = resultSet.getInt("id");
                return userId;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public User getUser(Integer id) {
        try {
            Connection connection = JDBCUtil.getConnection();
            String sql = "select * from t_user where id = ?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setInt(1, id);
            ResultSet resultSet = ps.executeQuery();

            while (resultSet.next()) {
                String username = resultSet.getString("username");
                String realName = resultSet.getString("realname");
                String cardId = resultSet.getString("cardid");
                Date birthday = resultSet.getDate("birthday");
                String phone = resultSet.getString("phone");
                String userInfo = resultSet.getString("userInfo");

                User user = new User(id, username, realName, cardId, birthday, phone, userInfo);
                return user;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
